package me.leiusual.sharing.ui.store.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import kotlinx.android.synthetic.main.dialog_store_category.*
import me.leiusual.sharing.R
import me.leiusual.sharing.api.model.StoreCategories
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.MainViewModel
import me.leiusual.sharing.ui.base.AbstractBottomSheetDialog
import me.leiusual.sharing.ui.helper.HandleObserver
import me.leiusual.sharing.ui.store.adapter.StoreCategoryAdapter
import me.leiusual.sharing.ui.store.adapter.StoreCategoryViewHolder
import timber.log.Timber
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-25
 * description:
 */
class StoreCategoryDialog : AbstractBottomSheetDialog() {

  companion object {
    private const val STORE_ID = "store_id"

    fun newInstance(storeId: Long): StoreCategoryDialog {
      return StoreCategoryDialog().apply {
        val args = Bundle()
        args.putLong(STORE_ID, storeId)
        arguments = args
      }
    }
  }

  @Inject
  lateinit var factory: ViewModelProvider.Factory

  private val mainViewModel: MainViewModel by activityViewModels { factory }

  private val viewModel: StoreCategoryViewModel by viewModels { factory }

  private val storeId: Long
    get() = arguments?.getLong(STORE_ID) ?: viewModel.store.currentStoreId

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.dialog_store_category, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    recycler_view_0.adapter = StoreCategoryAdapter(object : StoreCategoryViewHolder.Listener {
      override fun onCategoryClicked(category: StoreCategories.Response.Category.Item) {

        // 隐藏子类
        recycler_view_1.isVisible = false
        recycler_view_2.isVisible = false

        // 记录id
        mainViewModel.category(category, 0)

        // 获取子类，等于 0 时为全部分类，不需要获取子类
        if (category.id > 0) {
          viewModel.storeCategories1(category.id, storeId)
        }
      }
    })

    recycler_view_1.adapter = StoreCategoryAdapter(object : StoreCategoryViewHolder.Listener {
      override fun onCategoryClicked(category: StoreCategories.Response.Category.Item) {

        // 隐藏子类
        recycler_view_2.isVisible = false

        // 记录id
        mainViewModel.category(category, 1)

        viewModel.storeCategories2(category.id, storeId)
      }
    })

    recycler_view_2.adapter = StoreCategoryAdapter(object : StoreCategoryViewHolder.Listener {
      override fun onCategoryClicked(category: StoreCategories.Response.Category.Item) {

        // 记录id
        mainViewModel.category(category, 2)
      }
    })

    observeData()
    viewModel.storeCategories0(null, storeId)
  }

  private fun observeData() {
    viewModel.storeCategories0.observe(viewLifecycleOwner, HandleObserver {
      Timber.v("storeCategories = %s", it)
      when (it) {
        is Data.Loading -> {
          state_constraint_layout.showProgress()
        }
        is Data.Error -> {
          state_constraint_layout.showError(it.error.message)
        }
        is Data.Success -> {
          recycler_view_0.isVisible = true
          val adapter = recycler_view_0.adapter as StoreCategoryAdapter

          // 添加一个：全部分类
          val list = it.data.toCategoryList().toMutableList()
          list.add(0, StoreCategories.Response.Category.Item(0, "全部分类"))
          adapter.submitList(list)
        }
      }
    })

    viewModel.storeCategories1.observe(viewLifecycleOwner, HandleObserver {
      Timber.v("storeCategories1 = %s", it)
      when (it) {
        is Data.Success -> {
          recycler_view_1.isVisible = true
          val adapter = recycler_view_1.adapter as StoreCategoryAdapter
          adapter.submitList(it.data.toCategoryList())
        }
      }
    })

    viewModel.storeCategories2.observe(viewLifecycleOwner, HandleObserver {
      Timber.v("storeCategories2 = %s", it)
      when (it) {
        is Data.Success -> {
          recycler_view_2.isVisible = true
          val adapter = recycler_view_2.adapter as StoreCategoryAdapter
          adapter.submitList(it.data.toCategoryList())
        }
      }
    })
  }
}